﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Mono.Cecil;
using System.IO;

namespace TestApp
{
    class Program
    {
        static string _thisAsmLoc = Assembly.GetExecutingAssembly().Location;
        static string _newAsmLoc = _thisAsmLoc.Replace(".exe", "2.exe");

        // When TestApp.exe is executed, it should modify itself to generate TestApp2.exe.
        // When TestApp2.exe is executed, it should sucessfully run and write two lines to the console.
        static void Main(string[] args)
        {
            if (_thisAsmLoc.EndsWith("2.exe"))
            {
                var oc = new OtherClass();
                string t = oc.M2(true);
                Console.WriteLine(t);
                string f = oc.M2(false);
                Console.WriteLine(f);

                Console.Read();
            }
            else
            {
                // Tests CryptStr.ModuleWeaver by running it against the currently
                // executing assembly.

                var thisAsm = AssemblyDefinition.ReadAssembly(_thisAsmLoc);

                var weaver = new CryptStr.ModuleWeaver();
                weaver.ModuleDefinition = thisAsm.MainModule;
                weaver.Execute();

                File.Delete(_newAsmLoc);
                thisAsm.Write(_newAsmLoc);
            }
        }

        static string TestMethod(string input)
        {
            return input.ToUpper();
        }
    }

    class OtherClass
    {
        public string M2( bool b)
        {
            if (b)
            {
                return ("b was true.");
            }

            string str = "b was false";

            return str;
        }
    }
}
